<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
        <!--  <a-col :span="24">
            <a-form-model-item label="版本" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lockVersion">
              <a-input-number v-model="model.lockVersion" placeholder="请输入版本" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="删除标记0正常 1删除" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="delFlag">
              <a-input-number v-model="model.delFlag" placeholder="请输入删除标记0正常 1删除" style="width: 100%" />
            </a-form-model-item>
          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="店铺ID OTO" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shopId">
<!--              <a-input v-model="model.shopId" placeholder="请输入店铺ID OTO"  ></a-input>-->
              <a-select type="list_multi" v-model="model.shopId" placeholder="请选择店铺" >

                <a-select-option v-for="(i, k) in shopList" :value="i.id" :key="i.id">
                  {{i.name}}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
         <!-- <a-col :span="24">
            <a-form-model-item label="日单量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderDay">
              <a-input-number v-model="model.orderDay" placeholder="请输入日单量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="总单量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderTotal">
              <a-input-number v-model="model.orderTotal" placeholder="请输入总单量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="月单量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderMonthly">
              <a-input-number v-model="model.orderMonthly" placeholder="请输入月单量" style="width: 100%" />
            </a-form-model-item>
          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="自定义总单量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="textTotal">
              <a-input-number v-model="model.textTotal" placeholder="请输入自定义总单量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="自定义月单量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="textMonthly">
              <a-input-number v-model="model.textMonthly" placeholder="请输入自定义月单量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="配送时长(分钟)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="duration">
              <a-input-number v-model="model.duration" placeholder="请输入配送时长"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="人均消费(元)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="perCost">
              <a-input v-model="model.perCost" placeholder="请输入人均消费"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="起送价格(元)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startFare">
              <a-input v-model="model.startFare" placeholder="请输入起送价格"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="免费配送条件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="freeDistribution">
              <a-input v-model="model.freeDistribution" placeholder="请输入免费配送条件"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="最大配送距离(km)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mxDistance">
              <a-input v-model="model.mxDistance" placeholder="请输入最大配送距离"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="免费配送距离(km)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="freeDistance">
              <a-input v-model="model.freeDistance" placeholder="请输入免费配送距离"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="店铺统一打包费(元)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pack">
              <a-input v-model="model.pack" placeholder="请输入店铺统一打包费"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="订单配送模式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="oderModel">
              <a-input v-model="model.oderModel" placeholder="请输入订单配送模式"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="预约单配送模式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="appoModel">
              <a-input v-model="model.appoModel" placeholder="请输入预约单配送模式"  ></a-input>
            </a-form-model-item>
          </a-col>
          <!--<a-col :span="24">
            <a-form-model-item label="主频道ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="channelId">
              <j-dict-select-tag type="list" v-model="model.channelId" dictCode="mk_shop_channel,name,id" placeholder="请选择主频道ID" />
            </a-form-model-item>
          </a-col>-->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'ShopTakeoutForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
          shopList: [],
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
            shopId: [
                { required: true, message: '请选择门店!' },
            ],
            perCost: [
                { required: false, message: '请正确输入人均消费!' },
                {
                    pattern: /^(([1-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/,
                    message: '请正确输入人均消费!（金额>0.01）'
                },
            ],
            startFare: [
                { required: false, message: '请输入起送价格!' },
                {
                    pattern: /^(([1-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/,
                    message: '请正确输入起送价格!（金额>0.01）'
                },
            ],
            pack: [
                { required: false, message: '请正确输入打包费!' },
                {
                    pattern: /^(([1-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/,
                    message: '请正确输入打包费!（金额>0.01）'
                },
            ],
        },
        url: {
          add: "/shop/shopTakeout/add",
          edit: "/shop/shopTakeout/edit",
          queryById: "/shop/shopTakeout/queryById",
            getShopListUrl: '/shop/shopBasic/selectShopList'
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
        this.getShopListUrl();
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
        getShopListUrl(){
            this.shopList = [];
            getAction(this.url.getShopListUrl).then(res => {
                if (res.success) {
                    let result = res.result
                    result.forEach((e) => {
                        this.shopList.push(e)
                    })
                }
            })
            console.log(this.shopList)

        },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
          this.getShopListUrl();
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>